package org.tr.demo;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

import java.io.FileReader;
import java.util.Date;
import java.util.Properties;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * User: Lior Ben Zeev
 * Date: Aug 8, 2011
 */
public class TestApp {

    private static final Logger log = Logger.getLogger(TestApp.class);

    public static void main(String[] args) throws Exception {


        Properties logProperties = new Properties();

        logProperties.load(new FileReader("log4j.properties"));
        PropertyConfigurator.configure(logProperties);
        Thread.sleep(60*1000);


        System.out.println("Before Sleep");
        long st = System.currentTimeMillis();
        for (int i=0; i<10; i++) {
            Thread.sleep(1000);
            System.out.println(new Date(System.currentTimeMillis()));
        }
        long et = System.currentTimeMillis();
        long tt=et-st;

        System.out.println("After Sleep "+tt);

        System.out.println("sf");
        SchedulerFactory sf = new StdSchedulerFactory();
        System.out.println("after sf");
        Scheduler sched = sf.getScheduler();

        System.out.println("job");
        JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();

        System.out.println("trigger");
        Trigger trigger = newTrigger()
           .withIdentity("trigger1", "group1")
           .startAt(new Date())
           .withSchedule(cronSchedule("0 0 * * * ?")) //0 seconds, 0 minutes, any hour, any day of month, any month, any day of week
           .build();

        System.out.println("scheduleJob, currentTime: "+new Date());
        sched.scheduleJob(job, trigger);
//
//        // Start up the scheduler (nothing can actually run until the
//        // scheduler has been started)
        System.out.println("sched.start");
        sched.start();
//
        System.out.println("Thread.sleep");
        Thread.sleep(1000L*60L*60L*3L);
        sched.shutdown(true);
    }
}
